﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Overby;
using System.Collections;

namespace WebFormsDemo
{
    public partial class SortQueueDemoPage : System.Web.UI.Page
    {
        private IEnumerable<Person> PeopleData
        {
            get
            {
                return ViewState["PeopleData"] as IEnumerable<Person>;
            }
            set
            {
                ViewState["PeopleData"] = value;
            }
        }

        public SortQueue SortQueue
        {
            get
            {
                return ViewState["SortQueue"] as SortQueue;
            }
            set
            {
                ViewState["SortQueue"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SortQueue = new SortQueue(4, true, "Id");
                BuildData();
                BindData();
            }
        }

        private void BindData()
        {
            gridView1.DataSource = SortQueue.Sort(PeopleData.AsQueryable());
            gridView1.DataBind();
        }

        private void BuildData()
        {
            Random random = new Random();
            List<Person> data = new List<Person>();

            for (int i = 0; i < 1000; i++)
            {
                data.Add(Person.Create(i + 1, random));
            }

            PeopleData = data;
        }

        protected void Sorting(object sender, GridViewSortEventArgs e)
        {
            SortQueue.Add(e.SortExpression);
            BindData();
        }

        [Serializable]
        private class Person
        {
            public int Id { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public int Age { get; set; }

            private Person() { }

            public static Person Create(int id, Random random)
            {
                return new Person
                {
                    Id = id,
                    FirstName = GetFirstName(random),
                    LastName = GetLastName(random),
                    Age = random.Next(80)
                };
            }

            private static string GetLastName(Random random)
            {
                switch (random.Next(4))
                {
                    case 0: return "Arnold";
                    case 1: return "Franklin";
                    case 2: return "Martin";
                    case 3: return "Smith";
                    default: throw new Exception();
                }
            }

            private static string GetFirstName(Random random)
            {
                switch (random.Next(10))
                {
                    case 0: return "Amy";
                    case 1: return "Carl";
                    case 2: return "Donald";
                    case 3: return "Gwen";
                    case 4: return "Hank";
                    case 5: return "Joanna";
                    case 6: return "Nate";
                    case 7: return "Paula";
                    case 8: return "Ronnie";
                    case 9: return "Sarah";
                    default: throw new Exception();
                }
            }
        }
    }
}